package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.core.constants.HttpStatus;
import com.demo.core.domain.Result;
import com.demo.core.exception.ParameterException;
import com.demo.domain.News;
import com.demo.service.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @Operation(summary = "得到新闻展示列表", description = "不需分页则current和size值小于0")
    @PostMapping("/getDisplayNewsList")
    public Result getDisplayNewsList(Integer current, Integer size, Integer type) throws Exception {
        Page page = newsService.getDisplayNewsList(current, size, type);
        return page.getRecords() != null && page.getRecords().size() > 0 ?
                Result.success(HttpStatus.SUCCESS, "查找成功", page) :
                Result.success(HttpStatus.SUCCESS, "无相关信息", new Page<>());
    }

    /**
     * 详细的新闻内容
     *
     * @param newsId
     * @return
     * @throws ParameterException
     */
    @GetMapping(value = "/newsContent")
    public Result getNewsContent(@RequestParam Long newsId) throws ParameterException {
        if (newsId == null || newsId <= 0L) {
            throw new ParameterException("postId为空或非法");
        }
        News news = newsService.getNewsContent(newsId);
        return news == null ?
                Result.success(HttpStatus.NO_CONTENT, "新闻不存在", null) :
                Result.success(HttpStatus.SUCCESS, "获取帖子内容成功", news);
    }


    @Operation(summary = "得到最新的六条系友新闻")
    @GetMapping("/getLateSixFriendsNews")
    public Result getLateSixFriendsNews() {
        List<News> list = newsService.getLateSixFriendsNew();
        return list != null && list.size() > 0 ?
                Result.success(HttpStatus.SUCCESS, "查找成功", list)
                : Result.success(HttpStatus.SUCCESS, "无相关信息", new ArrayList<>());
    }
}
